package com.java.test.system.templatemember;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.registrationtemplate.RegistrationTemplateDAO;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.system.templatemember.constant.TemplateCodeConst;
import com.java.test.system.templatemember.dto.TemplateAddDTO;
import com.java.test.system.templatemember.dto.TemplateQueryDTO;
import com.java.test.system.templatemember.dto.TemplateUpdateDTO;
import com.java.test.system.templatemember.entity.LanguageEntity;
import com.java.test.system.templatemember.entity.TemplateEntity;
import com.java.test.system.templatemember.entity.TerminalEntity;
import com.java.test.system.templatemember.vo.TemplateVO;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模板管理Service
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Service
public class TemplateService {

    private TemplateDAO templateDAO;
    @Autowired
    public void setTemplateDAO(TemplateDAO templateDAO) {
        this.templateDAO = templateDAO;
    }

    private RegistrationTemplateDAO registrationTemplateDAO;
    @Autowired
    public void setRegistrationTemplateDAO(RegistrationTemplateDAO registrationTemplateDAO) {
        this.registrationTemplateDAO = registrationTemplateDAO;
    }

    /**
     * 分页查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<PageResultDTO<TemplateVO>> getSelectByList(TemplateQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<TemplateEntity> entityList = templateDAO.getSelectByList(queryDTO);
        for (TemplateEntity entity:entityList) {
            String languageName = " ";
            String areaName = " ";
            String terminalName = " ";
            List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds("0,"+entity.getAreaId());
            for (AreaEntity q:areaEntityList) {
                areaName+=q.getName()+",";
            }
            entity.setAreaName(areaName.substring(0,areaName.length()-1));

            List<LanguageEntity> languageEntityList = templateDAO.getLanguageListByIds("0,"+entity.getLanguage());
            for (LanguageEntity q:languageEntityList) {
                languageName+=q.getName()+",";
            }
            entity.setLanguageName(languageName.substring(0,languageName.length()-1));

            List<TerminalEntity> terminalEntityList = templateDAO.getTerminalListByIds("0,"+entity.getTerminal());
            for (TerminalEntity q:terminalEntityList) {
                terminalName+=q.getName()+",";
            }
            entity.setTerminalName(terminalName.substring(0,terminalName.length()-1));
        }
        PageResultDTO<TemplateVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, TemplateVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /***
     * 保存
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> save(TemplateAddDTO addDTO) {
        TemplateEntity entity = templateDAO.getByGName(addDTO);
        if (entity != null) {
            return ResponseDTO.wrap(TemplateCodeConst.ALREADY_EXIST);
        }
        templateDAO.insertSave(addDTO);
        return ResponseDTO.succ();
    }

    /**
     * 查询数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<TemplateVO> selectById(String id) {
        String areaName = " ";
        TemplateEntity entity = templateDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(TemplateCodeConst.NOT_EXIST);
        }
        List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
        for (AreaEntity q:areaEntityList) {
            areaName+=q.getName()+",";
        }
        entity.setAreaName(areaName.substring(0,areaName.length()-1));
        TemplateVO configDTO = SmartBeanUtil.copy(entity, TemplateVO.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 修改数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> updateById(TemplateUpdateDTO updateDTO) {
        TemplateEntity entity = templateDAO.getById(updateDTO.getId());
        if (entity == null) {
            return ResponseDTO.wrap(TemplateCodeConst.NOT_EXIST);
        }
        TemplateEntity alreadyEntity = templateDAO.getByKeyExcludeId(updateDTO);
        if (alreadyEntity != null) {
            return ResponseDTO.wrap(TemplateCodeConst.ALREADY_EXIST);
        }
        templateDAO.updateById(updateDTO);
        return ResponseDTO.succ();
    }

    /**
     * 删除数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> deleteById(String id) {
        TemplateEntity entity = templateDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(TemplateCodeConst.NOT_EXIST);
        }
        templateDAO.deleteById(id);
        return ResponseDTO.succ();
    }
}
